module game {
    class BattleHandle {
        battleInfo: any
        battleView:view.BattleView
        public constructor() {
            this.battleInfo = {
                playerInfo1: null,
                playerInfo2: null
            }
        }
        //设置基本信息
        public setBaseInfo(player1,player2,hpPB1,hpPB2,battleView: view.BattleView) {
            this.battleInfo.playerInfo1 = {
                fighter: player1.fightAttri,
                skillRelease: [],
                state1: 0, //0无状态1cd中
                state2: 0, //0无状态1被控制中
                hpPB: hpPB1,
                skillComponents: [],
            }
            this.battleInfo.playerInfo2 = {
                fighter: player2.fightAttri,
                skillRelease: [],
                state1: 0, //0无状态1cd中
                state2: 0, //0无状态1被控制中
                hpPB: hpPB2,
                skillComponents: [],
            }
            this.battleView = battleView
        }
        //开始战斗
        public start() {
            for(var i in this.battleInfo.playerInfo1.skillComponents) {
                var skillComponent = this.battleInfo.playerInfo1.skillComponents[i]
                this.releaseSkill(this.battleInfo.playerInfo1,this.battleInfo.playerInfo2,skillComponent)
            }
            for(var i in this.battleInfo.playerInfo2.skillComponents) {
                var skillComponent = this.battleInfo.playerInfo2.skillComponents[i]
                this.releaseSkill(this.battleInfo.playerInfo2,this.battleInfo.playerInfo1,skillComponent)
            }
        }
        //释放技能
        private releaseSkill(playerInfo1,playerInfo2,skillComponent) {
            var self = this
            if(playerInfo1.fighter.hp <= 0 || playerInfo1.fighter.hp <= 0) { 
                return
            }
            skillComponent.startSkill()
            var idTimeout: any = egret.setTimeout(function() {
                playerInfo1.skillRelease.push(skillComponent)
                self.attack(playerInfo1,playerInfo2)
            },this, skillComponent.skillCd)
        }
        //攻击
        private attack(attackerInfo,defenderInfo) {
            var self = this
            if(attackerInfo.fighter.hp <= 0 || defenderInfo.fighter.hp <= 0) {
                return
            }
            
            if(attackerInfo.state1 !== 0 && attackerInfo.state2 !== 1) {
                return
            }
            if(attackerInfo.skillRelease.length === 0) {
                return
            }
            var skillCon = attackerInfo.skillRelease.shift()
            var skillId = skillCon.skillId
            var damage = skillHandle[skillId](attackerInfo.fighter,defenderInfo.fighter)
            defenderInfo.fighter.hp -= damage
            defenderInfo.hpPB.value = defenderInfo.fighter.hp
            if(defenderInfo.fighter.hp <= 0) {
                this.battleView.close()
                return
            }
            attackerInfo.state1 = 1
            this.releaseSkill(attackerInfo,defenderInfo,skillCon)
            for(var i in attackerInfo.skillComponents) {
                var skillComponent = attackerInfo.skillComponents[i]
                skillComponent.startPublic()
            }
            var idTimeout: any = egret.setTimeout(function() {
                attackerInfo.state1 = 0
                for(var i in attackerInfo.skillComponents) {
                    var skillComponent = attackerInfo.skillComponents[i]                        
                    skillComponent.resetPublic()
                }
                self.attack(attackerInfo,defenderInfo)
            },this, 800)
        }
        //计算伤害
        public calcDamage(attacker: FightAttri,defender: FightAttri):number {
            var damage: number = attacker.attack * attacker.attack / (defender.defend + attacker.attack) //计算伤害
            if(damage <= 0) {
                damage = 1
            }
            damage = Math.ceil(damage)
            return damage
        }
    }
    export var battleHandle = new BattleHandle()
}
